package util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

import data.Point;

public class ReadMap {
	private String fileName;
	private BufferedReader in;
	private Point start;
	private Point end;

	private int rows;
	private int cols;

	private int[][] matrix;

	public ReadMap(String fileName) {
		this.fileName = fileName;
	}

	private void readHeader() throws IOException {
		//初始化读取流
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 读取起点和终点
		String startStr = in.readLine();
		String endStr = in.readLine();
		if (startStr != null && startStr.length() != 0) {
			String[] words = startStr.split(",");
			start = new Point(Integer.parseInt(words[0]), Integer.parseInt(words[1]));
		}
		if (endStr != null && endStr.length() != 0) {
			String[] words = endStr.split(",");
			end = new Point(Integer.parseInt(words[0]), Integer.parseInt(words[1]));
		}
		// 读入行列
		rows = Integer.parseInt(in.readLine());
		cols = Integer.parseInt(in.readLine());
	}

	private void readMatrix() throws IOException {
		matrix = new int[rows][cols];
		for (int i = 0; i < rows; ++i) {
			String[] words = in.readLine().split(" ");
			for(int j = 0;j < cols; ++j) {
				matrix[i][j] = Integer.parseInt(words[j]);
			}
		}
		
		if(start != null) {
			matrix[start.getY()][start.getX()] = 2;
		}
		
		if(end != null) {
			matrix[end.getY()][end.getX()] = 3;
		}
	}

	private void close() throws IOException {
		in.close();
	}
	
	public void read() {
		try {
			readHeader();
			readMatrix();
			close();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "file not exist", "Message", JOptionPane.ERROR_MESSAGE);
		}
	}

	public static void main(String[] args) {
		ReadMap read = new ReadMap("2-10-39-39.txt");
		read.read();
		System.out.println(read.getRows());
	}
	public Point getStart() {
		return start;
	}

	public void setStart(Point start) {
		this.start = start;
	}

	public Point getEnd() {
		return end;
	}

	public void setEnd(Point end) {
		this.end = end;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getCols() {
		return cols;
	}

	public void setCols(int cols) {
		this.cols = cols;
	}

	public int[][] getMatrix() {
		return matrix;
	}

	public void setMatrix(int[][] matrix) {
		this.matrix = matrix;
	}
}
